/*
 * Mapper.h
 *
 *  Created on: 2011-7-3
 *      Author: yong
 */

#ifndef MAPPER_H_
#define MAPPER_H_
#include<iostream>
#include "Typedef.h"
#include "Config.h"
#include "KeyValue.h"
#include <boost/mpi.hpp>
namespace distrim
{

template<typename K, typename V, typename P>
class Mapper
{
	friend class KeyValuePage<K, V, P> ;
public:
	typedef K key_type;
	typedef V value_type;
	typedef P param_type;

public:
	Mapper(KeyValuePage<K, V, P> *ptrKeyValuePage, const Config &config,
			P *ptrParam) :
		m_ptrKeyValuePage(ptrKeyValuePage), m_config(config)
	{
	}
	Mapper()
	{
	}
	virtual ~Mapper()
	{
	}
	/**
	 * Must be ovrridden by sub classes.
	 */
	virtual void Map(const KeyValue<K, V> &kv) = 0;
	/**
	 *
	 */
	inline const Config &GetConfig() const
	{
		return m_config;
	}

	/**
	 *
	 */
	inline void SetConfig(const Config &config)
	{
		m_config = config;
	}

	/**
	 *
	 */
	inline KeyValuePage<K, V, P> *GetKeyValuePage() const
	{
		return m_ptrKeyValuePage;
	}

	/**
	 *
	 */
	inline void SetKeyValuePage(KeyValuePage<K, V, P> *ptrKeyValuePage)
	{
		m_ptrKeyValuePage = ptrKeyValuePage;
	}

	inline void SetParam(P * ptrParam)
	{
		m_ptrParam = ptrParam;
	}

	inline P * GetParam() const
	{
		return m_ptrParam;
	}

private:
	inline void map()
	{
		size_t pairsCount = m_ptrKeyValuePage->GetPairsCount();
		for (size_t i = 0; i < pairsCount; ++i)
		{
			// Get each key-value pair to map.
			Map((*m_ptrKeyValuePage)[i]);
		}
	}

private:
	KeyValuePage<K, V, P> *m_ptrKeyValuePage;
	Config m_config;
	P *m_ptrParam;

};

}

#endif /* MAPPER_H_ */
